<template>
  <div class="login-container">
    <!-- 动态背景 -->
    <div class="bg-animation">
      <div class="floating-shapes">
        <div class="shape shape-1" />
        <div class="shape shape-2" />
        <div class="shape shape-3" />
        <div class="shape shape-4" />
        <div class="shape shape-5" />
      </div>
    </div>
    <!-- 主要内容区域 -->
    <div class="login-content">
      <!-- 左侧装饰区域 -->
      <div class="left-section">
        <div class="logo-section">
          <div class="logo-icon">
            <div class="tech-circle">
              <div class="inner-circle" />
            </div>
          </div>
          <h1 class="system-title">武耐财务</h1>
          <p class="system-subtitle">后台管理系统</p>
        </div>

        <div class="decorative-elements">
          <div class="tech-lines">
            <div class="line line-1" />
            <div class="line line-2" />
            <div class="line line-3" />
          </div>
          <div class="data-visualization">
            <div v-for="i in 8" :key="i" class="chart-bar" :style="{ height: `${Math.random() * 60 + 20}px` }" />
          </div>
        </div>
      </div>

      <!-- 右侧登录表单 -->
      <div class="right-section">
        <div class="login-form-container">
          <div class="form-header">
            <h2 class="welcome-text">欢迎登录</h2>
            <p class="welcome-subtitle">请输入您的账户信息</p>
          </div>
          <form class="login-form" @submit.prevent="handleLogin">
            <div class="input-group">
              <div class="input-wrapper">
                <div class="input-icon">
                  <svg viewBox="0 0 24 24" fill="currentColor">
                    <path d="M12 12c2.21 0 4-1.79 4-4s-1.79-4-4-4-4 1.79-4 4 1.79 4 4 4zm0 2c-2.67 0-8 1.34-8 4v2h16v-2c0-2.66-5.33-4-8-4z" />
                  </svg>
                </div>
                <input v-model="loginForm.username" type="text" placeholder="请输入用户名" class="form-input" required />
              </div>
            </div>

            <div class="input-group">
              <div class="input-wrapper">
                <div class="input-icon">
                  <svg viewBox="0 0 24 24" fill="currentColor">
                    <path d="M18,8A2,2 0 0,1 20,10V20A2,2 0 0,1 18,22H6A2,2 0 0,1 4,20V10A2,2 0 0,1 6,8H7V6A5,5 0 0,1 12,1A5,5 0 0,1 17,6V8H18M12,3A3,3 0 0,0 9,6V8H15V6A3,3 0 0,0 12,3Z" />
                  </svg>
                </div>
                <input v-model="loginForm.password" :type="showPassword ? 'text' : 'password'" placeholder="请输入密码" class="form-input" required />
                <div class="password-toggle" @click="showPassword = !showPassword">
                  <svg v-if="!showPassword" viewBox="0 0 24 24" fill="currentColor">
                    <path d="M12,9A3,3 0 0,0 9,12A3,3 0 0,0 12,15A3,3 0 0,0 15,12A3,3 0 0,0 12,9M12,17A5,5 0 0,1 7,12A5,5 0 0,1 12,7A5,5 0 0,1 17,12A5,5 0 0,1 12,17M12,4.5C7,4.5 2.73,7.61 1,12C2.73,16.39 7,19.5 12,19.5C17,19.5 21.27,16.39 23,12C21.27,7.61 17,4.5 12,4.5Z" />
                  </svg>
                  <svg v-else viewBox="0 0 24 24" fill="currentColor">
                    <path
                      d="M11.83,9L15,12.16C15,12.11 15,12.05 15,12A3,3 0 0,0 12,9C11.94,9 11.89,9 11.83,9M7.53,9.8L9.08,11.35C9.03,11.56 9,11.77 9,12A3,3 0 0,0 12,15C12.22,15 12.44,14.97 12.65,14.92L14.2,16.47C13.53,16.8 12.79,17 12,17A5,5 0 0,1 7,12C7,11.21 7.2,10.47 7.53,9.8M2,4.27L4.28,6.55L4.73,7C3.08,8.3 1.78,10 1,12C2.73,16.39 7,19.5 12,19.5C13.55,19.5 15.03,19.2 16.38,18.66L16.81,19.09L19.73,22L21,20.73L3.27,3M12,7A5,5 0 0,1 17,12C17,12.64 16.87,13.26 16.64,13.82L19.57,16.75C21.07,15.5 22.27,13.86 23,12C21.27,7.61 17,4.5 12,4.5C10.6,4.5 9.26,4.75 8,5.2L10.17,7.35C10.76,7.13 11.37,7 12,7Z"
                    />
                  </svg>
                </div>
              </div>
            </div>

            <div class="form-options">
              <label class="remember-me">
                <input v-model="loginForm.remember" type="checkbox" />
                <span class="checkmark" />
                记住密码
              </label>
              <a href="#" class="forgot-password">忘记密码？</a>
            </div>

            <button type="submit" class="login-btn" :disabled="loading">
              <span v-if="!loading">登录</span>
              <div v-else class="loading-spinner">
                <div class="spinner" />
                <span>登录中...</span>
              </div>
            </button>
          </form>

          <div class="form-footer">
            <p class="copyright">© 2024 武耐财务管理系统 版权所有</p>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import useMenuTreeStore from '@/store/menuTree'
import useUserStore from '@/store/user'

const router = useRouter()

// 响应式数据
const showPassword = ref(false)
const loading = ref(false)

const loginForm = reactive({
  username: '',
  password: '',
  code: '0312',
  remember: false,
})

// 登录处理
function handleLogin() {
  if (!loginForm.username || !loginForm.password) {
    return
  }

  loading.value = true
  httpRequest('/admin/login', loginForm)
    .then(res => {
      message.success('登录成功')
      sessionStorage.setItem('token', res.token)
      useUserStore().setUser(res)
      useMenuTreeStore().setMenuList(res.menuList)
      setTimeout(() => {
        // 重定向到首页
        router.push('/home')
      }, 500)
    })
    .catch(() => {
      loading.value = false
    })
}

// 组件挂载后的动画初始化
onMounted(() => {
  // 可以在这里添加更多的动画初始化逻辑
})
</script>

<style lang="less" scoped>
.login-container {
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
  width: 100vw;
  height: 100vh;
  overflow: hidden;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
}

// 动态背景
.bg-animation {
  position: absolute;
  top: 0;
  left: 0;
  z-index: 1;
  width: 100%;
  height: 100%;
}

.floating-shapes {
  position: relative;
  width: 100%;
  height: 100%;
}

.shape {
  position: absolute;
  background: rgb(255 255 255 / 10%);
  border-radius: 50%;
  animation: float 6s ease-in-out infinite;

  &.shape-1 {
    top: 20%;
    left: 10%;
    width: 80px;
    height: 80px;
    animation-delay: 0s;
  }

  &.shape-2 {
    top: 60%;
    left: 80%;
    width: 120px;
    height: 120px;
    animation-delay: 2s;
  }

  &.shape-3 {
    top: 80%;
    left: 20%;
    width: 60px;
    height: 60px;
    animation-delay: 4s;
  }

  &.shape-4 {
    top: 10%;
    left: 70%;
    width: 100px;
    height: 100px;
    animation-delay: 1s;
  }

  &.shape-5 {
    top: 40%;
    left: 5%;
    width: 140px;
    height: 140px;
    animation-delay: 3s;
  }
}

@keyframes float {
  0%,
  100% {
    opacity: 0.7;
    transform: translateY(0) rotate(0deg);
  }

  50% {
    opacity: 1;
    transform: translateY(-20px) rotate(180deg);
  }
}

// 主要内容区域
.login-content {
  position: relative;
  z-index: 2;
  display: flex;
  width: 90%;
  max-width: 1200px;
  height: 80vh;
  overflow: hidden;
  background: rgb(255 255 255 / 95%);
  backdrop-filter: blur(10px);
  border-radius: 20px;
  box-shadow: 0 20px 40px rgb(0 0 0 / 10%);
}

// 左侧装饰区域
.left-section {
  position: relative;
  display: flex;
  flex: 1;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 60px 40px;
  background: linear-gradient(135deg, #4facfe 0%, #00f2fe 100%);
}

.logo-section {
  margin-bottom: 60px;
  text-align: center;
}

.logo-icon {
  margin-bottom: 30px;
}

.tech-circle {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 120px;
  height: 120px;
  margin: 0 auto;
  border: 3px solid rgb(255 255 255 / 30%);
  border-radius: 50%;
  animation: rotate 10s linear infinite;

  .inner-circle {
    position: relative;
    width: 80px;
    height: 80px;
    background: rgb(255 255 255 / 20%);
    border-radius: 50%;

    &::before {
      position: absolute;
      top: 50%;
      left: 50%;
      width: 40px;
      height: 40px;
      content: '';
      background: white;
      border-radius: 50%;
      transform: translate(-50%, -50%);
    }
  }
}

@keyframes rotate {
  from {
    transform: rotate(0deg);
  }

  to {
    transform: rotate(360deg);
  }
}

.system-title {
  margin: 0 0 10px;
  font-size: 36px;
  font-weight: 700;
  color: white;
  text-shadow: 0 2px 4px rgb(0 0 0 / 10%);
}

.system-subtitle {
  margin: 0;
  font-size: 18px;
  color: rgb(255 255 255 / 90%);
}

.decorative-elements {
  width: 100%;
}

.tech-lines {
  margin-bottom: 40px;

  .line {
    height: 2px;
    margin: 10px 0;
    background: rgb(255 255 255 / 30%);
    border-radius: 1px;
    animation: pulse 2s ease-in-out infinite;

    &.line-1 {
      width: 80%;
      animation-delay: 0s;
    }

    &.line-2 {
      width: 60%;
      animation-delay: 0.5s;
    }

    &.line-3 {
      width: 90%;
      animation-delay: 1s;
    }
  }
}

@keyframes pulse {
  0%,
  100% {
    opacity: 0.3;
  }

  50% {
    opacity: 1;
  }
}

.data-visualization {
  display: flex;
  align-items: flex-end;
  justify-content: space-between;
  height: 80px;

  .chart-bar {
    width: 8px;
    background: rgb(255 255 255 / 40%);
    border-radius: 4px;
    animation: chart-pulse 3s ease-in-out infinite;

    &:nth-child(odd) {
      animation-delay: 0.5s;
    }
  }
}

@keyframes chart-pulse {
  0%,
  100% {
    transform: scaleY(0.5);
  }

  50% {
    transform: scaleY(1);
  }
}

// 右侧登录表单
.right-section {
  display: flex;
  flex: 1;
  align-items: center;
  justify-content: center;
  padding: 60px 40px;
}

.login-form-container {
  width: 100%;
  max-width: 400px;
}

.form-header {
  margin-bottom: 40px;
  text-align: center;
}

.welcome-text {
  margin: 0 0 10px;
  font-size: 32px;
  font-weight: 600;
  color: #333;
}

.welcome-subtitle {
  margin: 0;
  font-size: 16px;
  color: #666;
}

.login-form {
  .input-group {
    margin-bottom: 24px;
  }

  .input-wrapper {
    position: relative;
    display: flex;
    align-items: center;
  }

  .input-icon {
    position: absolute;
    left: 16px;
    z-index: 1;

    svg {
      width: 20px;
      height: 20px;
      color: #999;
    }
  }

  .form-input {
    width: 100%;
    height: 56px;
    padding: 0 50px;
    font-size: 16px;
    background: #f8f9fa;
    border: 2px solid #e1e5e9;
    border-radius: 12px;
    transition: all 0.3s ease;

    &:focus {
      background: white;
      border-color: #4facfe;
      outline: none;
      box-shadow: 0 0 0 3px rgb(79 172 254 / 10%);
    }

    &::placeholder {
      color: #999;
    }
  }

  .password-toggle {
    position: absolute;
    right: 16px;
    z-index: 1;
    cursor: pointer;

    svg {
      width: 20px;
      height: 20px;
      color: #999;
      transition: color 0.3s ease;
    }

    &:hover svg {
      color: #4facfe;
    }
  }
}

.form-options {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 32px;
}

.remember-me {
  display: flex;
  align-items: center;
  font-size: 14px;
  color: #666;
  cursor: pointer;

  input[type='checkbox'] {
    display: none;
  }

  .checkmark {
    position: relative;
    width: 18px;
    height: 18px;
    margin-right: 8px;
    border: 2px solid #ddd;
    border-radius: 4px;
    transition: all 0.3s ease;

    &::after {
      position: absolute;
      top: 2px;
      left: 6px;
      width: 4px;
      height: 8px;
      content: '';
      border: solid white;
      border-width: 0 2px 2px 0;
      opacity: 0;
      transition: opacity 0.3s ease;
      transform: rotate(45deg);
    }
  }

  input[type='checkbox']:checked + .checkmark {
    background: #4facfe;
    border-color: #4facfe;

    &::after {
      opacity: 1;
    }
  }
}

.forgot-password {
  font-size: 14px;
  color: #4facfe;
  text-decoration: none;
  transition: color 0.3s ease;

  &:hover {
    color: #2196f3;
  }
}

.login-btn {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 100%;
  height: 56px;
  font-size: 18px;
  font-weight: 600;
  color: white;
  cursor: pointer;
  background: linear-gradient(135deg, #4facfe 0%, #00f2fe 100%);
  border: none;
  border-radius: 12px;
  transition: all 0.3s ease;

  &:disabled {
    cursor: not-allowed;
    opacity: 0.7;
  }

  &:hover:not(:disabled) {
    box-shadow: 0 8px 25px rgb(79 172 254 / 30%);
    transform: translateY(-2px);
  }
}

.loading-spinner {
  display: flex;
  gap: 12px;
  align-items: center;

  .spinner {
    width: 20px;
    height: 20px;
    border: 2px solid rgb(255 255 255 / 30%);
    border-top: 2px solid white;
    border-radius: 50%;
    animation: spin 1s linear infinite;
  }
}

@keyframes spin {
  0% {
    transform: rotate(0deg);
  }

  100% {
    transform: rotate(360deg);
  }
}

.form-footer {
  margin-top: 40px;
  text-align: center;

  .copyright {
    margin: 0;
    font-size: 12px;
    color: #999;
  }
}

// 响应式设计
@media (width <= 768px) {
  .login-content {
    flex-direction: column;
    width: 95%;
    height: 90vh;
  }

  .left-section {
    padding: 40px 20px;
  }

  .right-section {
    padding: 40px 20px;
  }

  .system-title {
    font-size: 28px;
  }

  .welcome-text {
    font-size: 24px;
  }
}
</style>
